package com.itheima.producer;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/*
 * 发送消息
 * */
public class Producer_HelloWorld {
    public static void main(String[] args) throws IOException, TimeoutException {
        //1.创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        //2.设置参数
        factory.setHost("172.81.237.219");//ip地址
        factory.setPort(5672);
        factory.setVirtualHost("/itcast");//虚拟机
        factory.setUsername("heima");
        factory.setPassword("heima");
        //3.创建连接
        Connection connection = factory.newConnection();
        //4.创建channel
        Channel channel = connection.createChannel();
        //5.创建队列queue
        /*
         * String queue(队列名称),
         * boolean durable（是否持久化）,
         * boolean exclusive（是否独占or是否删除队列）,
         * boolean autoDelete（是否自动删除）,
         * Map<String, Object> arguments（参数）
         * */
        channel.queueDeclare("hello_world", true, false, false, null);
        //6.发送消息给queue
      /*
      String exchange,交换机名称
       String routingKey,路由名称
       BasicProperties props,配置信息
       byte[] body。发送的消息数据
      */
        String body = "hello rabbitmq~~";
        channel.basicPublish("", "hello_world", null, body.getBytes());
        //7.释放资源
        channel.close();
        connection.close();


    }
}
